import { Dialect } from 'sequelize';

const {
  NODE_ENV,
  DEV_DB_HOST,
  DEV_DB_USERNAME,
  DEV_DB_KEY,
  PRO_DB_HOST,
  PRO_DB_USERNAME,
  PRO_DB_KEY,
  TEST_DB_HOST,
  TEST_DB_USERNAME,
  TEST_DB_KEY
} = process.env;

interface DatabaseConfig {
  dialect?: Dialect,
  delegate: string,
  baseDir: string,
  host?: string,
  port: number,
  database: string,
  username?: string,
  password?: string,
  define: {
    timestamps: boolean,
    underscored: boolean,
  }
  logging?: boolean
}

export const development: DatabaseConfig = {
  dialect: 'mysql',
  delegate: 'model',
  baseDir: 'database/models',
  host: DEV_DB_HOST,
  port: 3306,
  database: 'psy',
  username: DEV_DB_USERNAME,
  password: DEV_DB_KEY,
  define: {
    timestamps: true,
    underscored: false,
  }
};

export const production: DatabaseConfig = {
  dialect: 'mysql',
  delegate: 'model',
  baseDir: 'database/models',
  host: PRO_DB_HOST,
  port: 3306,
  database: 'psy',
  username: PRO_DB_USERNAME,
  password: PRO_DB_KEY,
  define: {
    timestamps: true,
    underscored: false,
  }
};

export const test: DatabaseConfig = {
  dialect: 'mysql',
  delegate: 'model',
  baseDir: 'database/models',
  host: TEST_DB_HOST,
  port: 3306,
  database: 'psy_test',
  username: TEST_DB_USERNAME,
  password: TEST_DB_KEY,
  define: {
    timestamps: true,
    underscored: false,
  },
  logging: false
};
const databaseConfig: { _: DatabaseConfig | null } = { _: null };
if (NODE_ENV === 'production') {
  databaseConfig._ = production;
}

if (NODE_ENV === 'development') {
  databaseConfig._ = development;
}

if (NODE_ENV === 'test') {
  databaseConfig._ = test;
}

export default databaseConfig._;
